/**
 * @file Define BlueMP controller for all BlueMP related functions
 * 
 * @author Steve Gao<gaofu@parllay.com>
 */
const WebBaseController     = require('./web-base-controller');
const {SystemServices}      = require('../../constants');

class BlueMPController extends WebBaseController {

    get bluempService() {
        if (!this._bluempService) {
            this._bluempService = this.getSystemService(SystemServices.BLUEMP_SERVICE);
        }

        return this._bluempService;
    }

    get wallService() {
        if (!this._wallService) {
            this._wallService = this.getSystemService(SystemServices.WALL_SERVICE);
        }

        return this._wallService;
    }

    get name() {
        return 'BlueMPController';
    }

    get handlersPath() {
        return 'bluemp';
    }

    getRedirectURI(request) {
        let host = request.headers['host'];
        let redirectURI = `http://${host}/bluemp/authorize`;

        return redirectURI;
    }

    getAuthorizeURL(redirectURI) {
        return this.bluempService.getAuthorizeURL(redirectURI);
    }

    getAccessToken(type, params, parentCallback) {
        return this.bluempService.getAccessToken(type, params, (result) => {
            parentCallback(result);
        });
    }

    getEnterpriseInfo(accessToken, parentCallback) {
        return this.bluempService.getEnterpriseInfo(accessToken, (result) => {
            parentCallback(result);
        });
    }

    saveOrUpdateSystemUser(selfServiceUserId, internalOrderId, initialPlanId, name, email, password, refs, locale, thirdPartyId, parentCallback) {
        this.wallService.saveOrUpdateSystemUser(selfServiceUserId, internalOrderId, initialPlanId, name, email, password, refs, locale, thirdPartyId, (result) => {
            parentCallback(result);
        });
    }

    /**
     * Get image URL with CDN host
     * 
     * @name getCDNImageURL
     * @param {String} image - The original image URL
     * 
     * @returns {String}
     */
    getCDNImageURL(image) {
        return this.wallService.getCDNImageURL(image);
    }
}

exports.BlueMPController = BlueMPController;